//editormd
$(function () {
    var editor = editormd("test-editor", {
        width: "100%",
        height: 720,
        theme: 'dark',
        // width  : "100%",
        // height : "100%",
        path: "/static/editormd/lib/",
        //下面这一行将使用dark主题
        previewTheme: "dark"
    });
});


layui.use(['form', 'layedit'], function () {
    var form = layui.form;
    var layedit = layui.layedit;
    var $ = layui.jquery;
    //构建一个默认的编辑器
    var index = layedit.build('LAY_demo1');


    //编辑器外部操作
    var active = {
        content: function () {
            alert(layedit.getContent(index)); //获取编辑器内容
        }
        , text: function () {
            alert(layedit.getText(index)); //获取编辑器纯文本内容
        }
        , selection: function () {
            alert(layedit.getSelection(index));
        }
    };

    $('.site-demo-layedit').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    $.ajax({
        type: 'get',
        url: "/getCategoryList",
        // contentType: "application/x-www-form-urlencoded",
        dataType: "json",
        success: function (data) {
            if (data.data != "") {
                $("#articleCategory1").empty();
                var comment = $("#articleCategory1");
                $.each(data.data, function (index, obj) {
                    var li = $('<span>' + '<i class="fa fa-home fa-fw">' + '</i>' + obj['categoryname'] +'  '+ '</span>');
                    comment.append(li);
                })
                layer.msg("获取已有分类成功！", {icon: 6});
            } else {
                layer.msg("获取已有分类失败！", {icon: 5});
            }
        },
        error: function () {
            layer.msg("获取已有分类失败！", {icon: 5});
        }
    });
    $.ajax({
        type: 'get',
        url: "/getTagsList",
        // contentType: "application/x-www-form-urlencoded",
        dataType: "json",
        success: function (data) {
            if (data.data != "") {
                $("#articleTags1").empty();
                var comment = $("#articleTags1");
                $.each(data.data, function (index, obj) {
                    var li = $('<span>' + '<i class="fa fa-tags fa-fw">' + '</i>' + obj['tagname'] +'   '+ '</span>');
                    comment.append(li);
                })
                layer.msg("获取已有标签成功！", {icon: 6});
            } else {
                layer.msg("获取已有标签失败！", {icon: 5});
            }
        },
        error: function () {
            layer.msg("获取已有标签失败！", {icon: 5});
        }
    });



//监听文章提交
    form.on('submit(publishArticle)', function (data) {
        var title =$("[name='title']").val();
        var category =$("[name='category']").val();
        var tags =$("[name='tags']").val();
        var image =$("[name='image']").val();
        var description =$("[name='description']").val();
        var content =$("[name='content']").val();
        var reference=$("[name='reference']").val();
        var loadIndex = '';
        $.ajax({
            type:'post',
            url:'/publishArticle',
            dataType:'json',
            data:{
                title:title,
                category:category,
                tags:tags,
                image:image,
                description:description,
                content:content,
                reference:reference
            },
            beforeSend: function () {
                loadIndex = layer.load(1, {
                    shade: [0.1, '#fff']
                });
            },
            complete: function () {
                layer.close(loadIndex);
            },
            success:function (data) {

                layer.msg("发布文章成功！",{icon: 6});
            },
            error: function () {
                layer.msg("发布文章失败！",{icon: 5});
            }
        })
        return false;
    });

})

